<?php

namespace App\Http\Controllers;

use App\Models\UsersModel;

class UserController extends Controller
{
    public function getUserInfo()
    {
        $model = UsersModel::query();
        $user_info = $model->select([
            'username',
            'avatar',
            'sign',
            'is_vip',
            'vip_end_time',
            'is_meeting',
            'mobile'
        ])->find($this->user_id);

        $user_info['is_meeting'] = (bool)$user_info['is_meeting'];
        $user_info['mobile'] = cover_mobile($user_info['mobile']);
        return $this->success(['user_info' => $user_info]);
    }

    public function editMeetingStatus()
    {
        $is_meeting = $this->request->input('is_meeting');
        $model = UsersModel::query();

        $user = $model->where(['id' => $this->user_id])->first();

        if (!$user->is_vip || strtotime($user->vip_end_time) <= time()) {
            return $this->fail(CODE_ERR_AUTH_NOT_VIP);
        }

        $re = $user->where(['id' => $this->user_id])->update([
            'is_meeting' => (int)$is_meeting
        ]);
        if (!$re) {
            return $this->fail(CODE_ERR_SYSTEM, '操作失败');
        }

        return $this->success();
    }

    public function editUserInfo()
    {
        $username = $this->request->input('username', '');
        $sign = $this->request->input('sign', '');
        $avatar = $this->request->input('avatar', '');
        $update_data = [];
        if (!empty($username)) {
            $update_data['username'] = $username;
        }
        if (!empty($sign)) {
            $update_data['sign'] = $sign;
        }
        if (!empty($avatar)) {
            $update_data['avatar'] = $avatar;
        }

        if (empty($update_data)) {
            return $this->fail(CODE_ERR_PARAM, '参数错误');
        }

        $re = UsersModel::query()->where(['id' => $this->user_id])->update($update_data);
        if (!$re) {
            return $this->fail(CODE_ERR_SYSTEM, '操作失败');
        }

        return $this->success();
    }
}
